home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Documentation / Books / Learn Java on the Macintosh / Learn Java Projects / 14.03 - stand alone / SimpleDraw.java next >
Text File  |  1996-04-22  |  4KB  |  142 lines

  1. /* -------------------------------------------------------------
  2. This stand-alone application paints a circle or square of the color 
  3. you've chosen wherever you click. This application keeps a list of 
  4. the shapes you've drawn and paints all the shapes in the list when 
  5. it repaints.
  6.  
  7. Java's classes: Applet    (applet)
  8.                 Event     (awt)     user-generated action
  9.                 Graphics  (awt)     used for drawing
  10.                 Color     (awt)     defines colors
  11.                 Choice    (awt)     shape and color selection choices
  12.                 Vector    (util)    list of shapes
  13.  
  14. Custom classes: SimpleDraw
  15.                 Circle              defines and draws circles
  16.                 Square              defines and draws squares
  17.                 Shape                a common ancestor for circles and squares
  18.  
  19. ------------------------------------------------------------- */
  20.  
  21. import java.applet.Applet;
  22. import java.util.*;
  23. import java.awt.*;
  24.  
  25. public class SimpleDraw extends Applet {
  26.    Vector  drawnShapes;
  27.    Choice  shapeChoice;
  28.    Choice  colorChoice;
  29.    
  30.    /** Be able to run as a stand-alone application. */
  31.    public static void main(String[] args) {
  32.    
  33.       // create a new instance of this applet
  34.       SimpleDraw sd = new SimpleDraw();
  35.       
  36.       // initialize the applet
  37.       sd.init();
  38.       
  39.       // create a frame to hold this applet
  40.       Frame f = new Frame("SimpleDraw");
  41.       
  42.       // put the applet into the frame
  43.       f.add("Center", sd);
  44.       
  45.       // give the frame a default size
  46.       f.resize(200,200);
  47.       
  48.       // make the frame appear
  49.       f.show();
  50.    }
  51.    
  52.    /** Create the GUI. */
  53.    public void init() {
  54.       drawnShapes = new Vector();
  55.       
  56.       shapeChoice = new Choice();
  57.       shapeChoice.addItem("Circle");
  58.       shapeChoice.addItem("Square");
  59.       add(shapeChoice);
  60.       
  61.       colorChoice = new Choice();
  62.       colorChoice.addItem("Red");
  63.       colorChoice.addItem("Green");
  64.       colorChoice.addItem("Blue");
  65.       add(colorChoice);
  66.    }
  67.    
  68.    /** Draw all the shapes. */
  69.    public void paint(Graphics g) {
  70.       Shape s;
  71.       int numShapes;
  72.       
  73.       numShapes = drawnShapes.size();
  74.       for (int i = 0; i < numShapes; i++) {
  75.       
  76.          s = (Shape)drawnShapes.elementAt(i);
  77.          
  78.          // When the shape draws, circles and squares each invoke their own
  79.          // draw method, depending on which shape this is.
  80.          s.draw(g);  
  81.       }
  82.    }
  83.    
  84.    /** Create a new shape. */
  85.    public boolean mouseUp(Event e, int x, int y) {
  86.    
  87.       Shape s;  // This shape will be either a circle or a square.
  88.    
  89.       String shapeString = shapeChoice.getSelectedItem();
  90.       String colorString = colorChoice.getSelectedItem();
  91.       
  92.       if (shapeString.equals("Circle"))
  93.          s = new Circle();
  94.       else
  95.          s = new Square();
  96.       
  97.       if (colorString.equals("Red"))
  98.          s.color = Color.red;
  99.       else if (colorString.equals("Green"))
  100.          s.color = Color.green;
  101.       else
  102.          s.color = Color.blue;
  103.          
  104.       s.x = x;
  105.       s.y = y;
  106.       
  107.       drawnShapes.addElement(s);
  108.       
  109.       repaint();
  110.       
  111.       return true;
  112.    }
  113.  
  114. }
  115.  
  116. /** Shapes provide common characteristics for the circle and square. */
  117. abstract class Shape {
  118.    static public final int shapeRadius = 20;
  119.    
  120.    Color color;
  121.    int x;
  122.    int y;
  123.    
  124.    abstract void draw(Graphics g);
  125. }
  126.  
  127. /** Draws and maintains circle information. */
  128. class Circle extends Shape {
  129.    void draw(Graphics g) {
  130.       g.setColor(this.color);
  131.       g.fillOval(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);
  132.    }
  133. }
  134.  
  135. /** Draws and maintains square information. */
  136. class Square extends Shape{
  137.    void draw(Graphics g) {
  138.       g.setColor(this.color);
  139.       g.fillRect(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);  
  140.    }
  141. }
  142.